/**
 * 
 */
package com.et114.components.ftp;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.util.Assert;

import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPMessageCollector;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.et114.core.resources.FrameworkMessageManager;

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.File;



/**
 * @author guanhw
 */
public class FtpUtil {
	private static Log					logger				= LogFactory.getLog( FtpUtil.class );
	private String ip ; 
	private String userName ; 
	private String password ;
	
	public FtpUtil(){
		
	}
	
	public FtpUtil( String ip , String userName , String password ){
		this.ip = ip; 
		this.userName = userName ; 
		this.password = password ;
	}
	
	public void ftpTo( String directory , String fileName ) throws Exception {
		Assert.state( !( ip == null || "".equals( ip.trim() ) ), FrameworkMessageManager.get ( ).getMessage ( "ftp.ip.val" ) );
		Assert.state( !( userName == null || "".equals( userName.trim() ) ), FrameworkMessageManager.get ( ).getMessage ( "ftp.username.val" ) );
		Assert.state( !( fileName == null || "".equals( fileName.trim() ) ) , FrameworkMessageManager.get ( ).getMessage ( "ftp.filename.val" ) ) ; 
		Assert.state( !( directory == null || "".equals( directory.trim() ) ) , FrameworkMessageManager.get ( ).getMessage ( "ftp.directory.val" ) ) ; 
		if ( logger.isInfoEnabled() ) {
			logger.info( "ip::" + ip ) ; 
			logger.info( "userName::" + userName ) ; 
			logger.info( "password::" + password ) ;
			logger.info( "directory::" + directory ) ; 
			logger.info( "fileName::" + fileName ) ; 
		}
		int slashIndex = fileName.lastIndexOf( '/' );
		int backslashIndex = fileName.lastIndexOf( '\\' );
		slashIndex = slashIndex > backslashIndex ? slashIndex : backslashIndex;
		String shortFileName = fileName.substring( slashIndex + 1 );
		File _file = new File( fileName );
		if ( !_file .exists() ) {
			throw new Exception( fileName + FrameworkMessageManager.get ( ).getMessage ( "ftp.file.notexists" ) );
		}
		if ( !_file.canRead() ) {
			throw new Exception( fileName + FrameworkMessageManager.get ( ).getMessage ( "ftp.file.cannotread" ) );
		}
		InputStream stream = new FileInputStream( _file );
		FTPClient ftp = null;
		FTPMessageCollector listener = null;
		ftp = new FTPClient( ip );
		listener = new FTPMessageCollector();
		ftp.setMessageListener( listener );
		ftp.login( userName, password );
		ftp.setConnectMode( FTPConnectMode.PASV );
		ftp.setType( FTPTransferType.ASCII );
		ftp.put( stream, directory + "/" + shortFileName );
		ftp.quit();

		if ( null != listener ) {
			String messages = listener.getLog();
			logger.info( messages );
		}
	}

	public String getIp ( ) {
		return ip;
	}
	public void setIp ( String ip ) {
		this.ip = ip;
	}
	public String getUserName ( ) {
		return userName;
	}
	public void setUserName ( String userName ) {
		this.userName = userName;
	}
	public String getPassword ( ) {
		return password;
	}
	public void setPassword ( String password ) {
		this.password = password;
	}
	
	public static void main( String[] ss ) {
		FtpUtil fu = new FtpUtil();
		try {
			fu.setIp( "1"  ) ; 
			fu.setUserName( "2" ) ; 
			fu.setPassword( "" ) ;
			fu.ftpTo( "/" , "" ) ; 			
		}catch ( Exception e ) {
			logger.error( e.getMessage() , e ) ; 
		}
	}
}
